//
//  BrandDetailCtrl.m
//  cutter
//
//  Created by mac on 16/1/20.
//  Copyright © 2016年 e360. All rights reserved.
//

#import "BrandDetailCtrl.h"

@implementation BrandDetailImage
@end


@implementation BrandDetailCell
-(void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected) {
        [self setBorderWidth:2];
    }else{
        [self setBorderWidth:0];
    }
}
@end


@implementation BrandDetailCtrl
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.data count] > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
        [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    }
    //
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.imageView addGestureRecognizer:singleFingerOne];
}
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender{
    CGFloat alpha = self.backView.alpha==0 ? 1 : 0;
    [UIView beginAnimations:nil context:nil];
    [self.collectionView setAlpha:alpha];
    [self.backView setAlpha:alpha];
    [UIView commitAnimations];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.imageView) {
        NSInteger page = round(scrollView.contentOffset.x / scrollView.frame.size.width);
        //
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:page inSection:0];
        [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView==self.collectionView) {
        [self.imageView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.data count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *item = [self.data objectAtIndex:indexPath.item];
    if (collectionView == self.collectionView) {
        BrandDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        [AppData loadImage:[item objectForKey:@"thumb"] view:cell.imageView];
        return cell;
    }
    //
    BrandDetailImage *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    [AppData loadImage:[item objectForKey:@"photo"] view:cell.imageView.imageView];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        return CGSizeMake(80, 60);
    }
    return self.view.bounds.size;
}
- (IBAction)exitTouch:(id)sender {
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
